/***********************************************
/gen auto by zuitools
***********************************************/

#include "entry/EasyUIContext.h"
#include "mainActivity.h"
#include "utils/Log.h"

/*register activity*/
REGISTER_ACTIVITY(mainActivity);

static ZKButton* m_btnStart;
static ZKButton* m_btnAuto;
static ZKTextView* m_textAnimation;
static ZKTextView* m_textValue;
static ZKSeekBar* m_SeekValue;
static ZKSeekBar* m_SeekWave;
static ZKDiagram* m_zkDiagram;
static int textanimationIndex = 0;
static bool bStartAnimation = false;
static bool bWaveAuto = false;
#define DIAGRAM_SIZE 100
static MPPOINT sPoints[DIAGRAM_SIZE];

static void movePoints(MPPOINT* p,int size){
	for(int i =0;i < size-1;i++){
		p[i].y = p[i+1].y;
	}
	p[size-1].y = 0;
}

class MySeekBarChangeListener : public ZKSeekBar::ISeekBarChangeListener {
public:
	virtual void onProgressChanged(ZKSeekBar *pSeekBar, int progress) {
		switch (pSeekBar->getID()) {
		case ID_MAIN_SLIDER_1:
			if (m_textValue != NULL) {
				char str[10] = { 0 };
				sprintf(str, "%d", progress);
				m_textValue->setText(str);
			}
			break;
		case ID_MAIN_SLIDER_2:
			if(m_zkDiagram != NULL && (bWaveAuto== false)){
				movePoints(sPoints,DIAGRAM_SIZE);
				if(m_SeekWave != NULL){
					sPoints[DIAGRAM_SIZE-1].y = m_SeekWave->getProgress();
				}
				//LOGD("show Diagram....%d",m_SeekWave->getProgress());
				m_zkDiagram->setData(0,sPoints,DIAGRAM_SIZE);
				m_zkDiagram->setData(1,sPoints,DIAGRAM_SIZE);
			}
			break;
		}
	}

	virtual void onStartTrackingTouch(ZKSeekBar *pSeekBar) {

	}

	virtual void onStopTrackingTouch(ZKSeekBar *pSeekBar) {

	}
};
static MySeekBarChangeListener sListener;

mainActivity::mainActivity() {
	//todo add init code here
}
mainActivity::~mainActivity() {
	//todo add init file here
}
const char* mainActivity::getAppName() const{
	return "main.ftu";
}
void mainActivity::onCreate() {
	//TODO: add create init code 
	Activity::onCreate();

	/**
	 * 找到希望操作的控件指针
	 */
	m_btnStart = (ZKButton*)findControlByID(ID_MAIN_BUTTON_1);
	m_textAnimation = (ZKTextView*)findControlByID(ID_MAIN_LABEL_ANIMATION);
	m_textValue = (ZKTextView*)findControlByID(ID_MAIN_LABEL_2);
	m_SeekValue = (ZKSeekBar*)findControlByID(ID_MAIN_SLIDER_1);
	m_SeekWave = (ZKSeekBar*)findControlByID(ID_MAIN_SLIDER_2);
	m_zkDiagram = (ZKDiagram*)findControlByID(ID_MAIN_WAVEVIEW_1);
	m_btnAuto = (ZKButton*)findControlByID(ID_MAIN_BUTTON_AUTO);
	if(m_zkDiagram == NULL){
		LOGD("get ZKDiagram fail........");
	}
	/**
	 * 初始化波形图显示变量，x轴平均分配
	 * y轴数据默认为0
	 */
	for(int i =0;i < DIAGRAM_SIZE;i++){
		sPoints[i].x = (100*i)/DIAGRAM_SIZE;
		sPoints[i].y = 0;
	}

	/**
	 * 注册监听
	 */
	if(m_SeekValue != NULL){
		m_SeekValue->setSeekBarChangeListener(&sListener);
	}

	if(m_SeekWave != NULL){
		m_SeekWave->setSeekBarChangeListener(&sListener);
	}

	/***
	 * 设置定时器，用于波形显示和动画显示
	 */
	//设置20ms的定时器用于动画显示
	registerTimer(0, 40);
	// 设置50ms的定时器用于波形图显示
	registerTimer(1, 100);
}
void mainActivity::onClick(ZKBase *pBase) {
	//TODO: add widget onClik code 
	if(pBase->getID() == ID_MAIN_BUTTON_1){
		if(bStartAnimation == false){
			m_btnStart->setText("停止动画");
			m_btnStart->setSelected(true);
			bStartAnimation = true;
		}else{
			m_btnStart->setText("开始动画");
			m_btnStart->setSelected(false);
			bStartAnimation = false;
		}
	}else if(pBase->getID() == ID_MAIN_BUTTON_AUTO){
		if(bWaveAuto == false){
			m_btnAuto->setText("手动触发");
			bWaveAuto = true;
		}else{
			m_btnAuto->setText("自动触发");
			bWaveAuto = false;
		}
	}
	Activity::onClick(pBase);
}

bool mainActivity::onTimer(int id) {
	switch (id) {
	case 0:
		if (m_textAnimation != NULL && bStartAnimation) {
			textanimationIndex++;
			if (textanimationIndex > 59) {
				textanimationIndex = 0;
			}
			char buff[5];
			sprintf(buff, "%c", textanimationIndex + '0');
			m_textAnimation->setText(buff);
		}
		break;
	case 1:
		if (m_zkDiagram != NULL && bWaveAuto) {
			movePoints(sPoints, DIAGRAM_SIZE);
			if (m_SeekWave != NULL) {
				sPoints[DIAGRAM_SIZE - 1].y = m_SeekWave->getProgress();
			}
			//LOGD("show Diagram....%d",m_SeekWave->getProgress());
			m_zkDiagram->setData(0, sPoints, DIAGRAM_SIZE);
			//m_zkDiagram->setData(1,sPoints,DIAGRAM_SIZE);
		}
		break;
	}

	return true;
}
